﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyBuildingCMS.BLL.CoreEngine;

namespace EasyBuildingCMS.BuildingBlocks.BuildingBlockPlaceHolder
{
    public partial class BuildingBlockPlaceHolder : System.Web.UI.UserControl
    {
        public EasyBuildingCMS.BuildingBlocks.BuildingBlockPlaceHolder.BuildingBlocksContainer BuildingBlocks
        {
            get
            {
                if (this.BuildingBlocksContainer == null)
                {
                    return (BuildingBlocksContainer)this.LoadControl("BuildingBlocksContainer.ascx");
                }

                return this.BuildingBlocksContainer;
            }
        }

        public string Mode
        {
            get { return BLL.Globals.Mode; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Default)this.Page).LoadPageContent((int)(BLL.Helper.ToInt(BLL.Globals.CurrentPage)));
        }

        public void ClearBuildingBlocks()
        {
            this.BuildingBlocksContainer.Controls.Clear();
        }

        protected void HiddenButton_Click(object sender, EventArgs e)
        {       
        }

        protected void SortOrderButton_Click(object sender, EventArgs e)
        {
            BuildingBlockInstanceRepository buildingBlockInstanceRepository = new BuildingBlockInstanceRepository();

            string[] columns = SortOrderHiddenField.Value.Split('&');
            foreach (string column in columns)
            {
                string[] nameValuePair = column.Split('=');

                int i = 1;
                string[] values = nameValuePair[1].Split(',');
                foreach (string valueString in values)
                {
                    if (BLL.Helper.ToInt(valueString) != null)
                    {
                        buildingBlockInstanceRepository.UpdateSortOrder((int)(BLL.Helper.ToInt(valueString)), i);
                        i++;
                    }
                }

            }

            ((Default)this.Page).LoadPageContent((int)(BLL.Helper.ToInt(BLL.Globals.CurrentPage)));
            ((UpdatePanel)(this.Page.Master.FindControl("system_MainContentUpdatePanel"))).Update();
            
        }

      
        
    }
}